package com.anolesoft.twelvePlan.mapper;

import cn.hutool.core.util.StrUtil;
import com.anolesoft.twelvePlan.entity.MesGyInfoDO;
import com.anolesoft.twelvePlan.entity.MesHistoryFabinfoDO;
import com.anolesoft.twelvePlan.entity.MesWxInfoDO;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

/**
 * @author zyq
 * @date 2022/3/5
 */
@Mapper
public interface MesHistoryFabinfoMapper extends BaseMapper<MesHistoryFabinfoDO> {
    @SelectProvider(type = createSQL.class, method = "querySQL")
    List<MesHistoryFabinfoDO> queryHistoryFabinfo(@Param("mesHistoryFabinfoDO") MesHistoryFabinfoDO mesHistoryFabinfoDO, Page<MesHistoryFabinfoDO> page);
    public static class createSQL {
        public static String querySQL(@Param("mesHistoryFabinfoDO") MesHistoryFabinfoDO mesHistoryFabinfoDO, Page<MesHistoryFabinfoDO> page) {
            return new SQL() {
                {
                    StringBuffer sql = new StringBuffer();
                    sql.append(" * from mes_history_fabinfo a where a.deleteflag = 0 ");
                    sql.append(" and a.factory_id = #{mesHistoryFabinfoDO.factoryId, jdbcType=VARCHAR}");
                    if (!StrUtil.isEmptyOrUndefined(mesHistoryFabinfoDO.getFabrecordid())) {
                        sql.append(" and a.fabrecordid = trim(#{mesHistoryFabinfoDO.fabrecordid, jdbcType=VARCHAR})");
                    }
                    if (!StrUtil.isEmptyOrUndefined(mesHistoryFabinfoDO.getPartid())) {
                        sql.append(" and a.partid = trim(upper(#{mesHistoryFabinfoDO.partid, jdbcType=VARCHAR}))");
                    }
                    SELECT(sql.toString());
                    System.out.println("queryHistoryFabinfoSQL:" + sql.toString());
                }
            }.toString();
        }
    }

    //查询是否重复
    @SelectProvider(type = createSQL2.class, method = "repeatHistorySQL")
    int repeatHistory(@Param("mesHistoryFabinfoDO") MesHistoryFabinfoDO mesHistoryFabinfoDO);
    public static class createSQL2 {
        public static String repeatHistorySQL(@Param("mesHistoryFabinfoDO") MesHistoryFabinfoDO mesHistoryFabinfoDO){
            return new SQL(){
                {
                    StringBuffer sql = new StringBuffer();
                    sql.append(" count(*) from mes_history_fabinfo a where ");
                    sql.append(" a.fabrecordid = trim(upper(#{mesHistoryFabinfoDO.fabrecordid, jdbcType=VARCHAR})) ");
                    sql.append(" and a.deleteflag = 0");
                    if (!StrUtil.isEmptyOrUndefined(mesHistoryFabinfoDO.getObjuid())){
                        sql.append(" and a.objuid != #{mesHistoryFabinfoDO.objuid, jdbcType=VARCHAR}");
                    }
                    SELECT(sql.toString());
                    System.out.println(">>>>repeatHistorySQL:" + sql.toString());
                }
            }.toString();
        }
    }

    @Insert("insert into mes_history_fabinfo (fabrecordid, partid, jgnum, worker, worktype, createuid, createtime, factory_id) " +
            "values (trim(#{mesHistoryFabinfoDO.fabrecordid, jdbcType=VARCHAR}), trim(upper(#{mesHistoryFabinfoDO.partid, jdbcType=VARCHAR})), " +
            " #{mesHistoryFabinfoDO.jgnum, jdbcType=VARCHAR}, #{mesHistoryFabinfoDO.worker, jdbcType=VARCHAR}, " +
            " #{mesHistoryFabinfoDO.worktype, jdbcType=VARCHAR}, #{mesHistoryFabinfoDO.createuid, jdbcType=VARCHAR}, " +
            " sysdate, #{mesHistoryFabinfoDO.factoryId, jdbcType=VARCHAR})")
    int insertHistoryFabinfo(@Param("mesHistoryFabinfoDO") MesHistoryFabinfoDO mesHistoryFabinfoDO);

    @Update("update mes_history_fabinfo a set " +
            " a.fabrecordid = trim(#{mesHistoryFabinfoDO.fabrecordid, jdbcType=VARCHAR})," +
            " a.partid = trim(upper(#{mesHistoryFabinfoDO.partid, jdbcType=VARCHAR}))," +
            " a.jgnum = #{mesHistoryFabinfoDO.jgnum, jdbcType=INTEGER}," +
            " a.worker = #{mesHistoryFabinfoDO.worker, jdbcType=VARCHAR}," +
            " a.worktype = #{mesHistoryFabinfoDO.worktype, jdbcType=VARCHAR}," +
            " a.modifyuid = #{mesHistoryFabinfoDO.modifyuid, jdbcType=VARCHAR}," +
            " a.modifytime = sysdate " +
            " where a.objuid = #{mesHistoryFabinfoDO.objuid, jdbcType=VARCHAR}")
    int updateHistoryFabinfo(@Param("mesHistoryFabinfoDO") MesHistoryFabinfoDO mesHistoryFabinfoDO);

    @Update("update mes_history_fabinfo a set " +
            " a.deleteflag = 1, " +
            " a.deleteuid = #{mesHistoryFabinfoDO.deleteuid, jdbcType=VARCHAR}, " +
            " a.deletetime = sysdate " +
            " where a.objuid = #{mesHistoryFabinfoDO.objuid, jdbcType=VARCHAR}")
    int deleteHistoryFabinfo(@Param("mesHistoryFabinfoDO") MesHistoryFabinfoDO mesHistoryFabinfoDO);

    @SelectProvider(type = createSQL3.class, method = "exportSQL")
    int exportHistoryFabinfo(@Param("mesHistoryFabinfoDO") MesHistoryFabinfoDO mesHistoryFabinfoDO);
    public static class createSQL3 {
        public static String exportSQL(@Param("mesHistoryFabinfoDO") MesHistoryFabinfoDO mesHistoryFabinfoDO) {
            return new SQL() {
                {
                    StringBuffer sql = new StringBuffer();
                    sql.append(" count(*) from mes_history_fabinfo a where a.deleteflag = 0 ");
                    sql.append(" and a.factory_id = #{mesHistoryFabinfoDO.factoryId, jdbcType=VARCHAR}");
                    if (!StrUtil.isEmptyOrUndefined(mesHistoryFabinfoDO.getFabrecordid())) {
                        sql.append(" and a.fabrecordid = trim(#{mesHistoryFabinfoDO.fabrecordid, jdbcType=VARCHAR})");
                    }
                    if (!StrUtil.isEmptyOrUndefined(mesHistoryFabinfoDO.getPartid())) {
                        sql.append(" and a.partid = trim(upper(#{mesHistoryFabinfoDO.partid, jdbcType=VARCHAR}))");
                    }
                    SELECT(sql.toString());
                    System.out.println("exportSQL:" + sql.toString());
                }
            }.toString();
        }
    }
}
